﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for playListForm.xaml
    /// </summary>
    public partial class playListForm : Window
    {
        #region "Properties"
        private List<PlayList> playLists = null;
        private LibraryView libView = null;
        #endregion

        public playListForm(LibraryView parent, List<PlayList> list_playList)
        {
            playLists = list_playList;
            libView = parent;

            InitializeComponent();
        }

        private bool checkIfPlayListExists(String name)
        {
            foreach (PlayList item in playLists)
            {
                if (String.Compare(item.getName(), name, true) == 0)
                    return (true);
            }
            if (String.Compare(name, "Library", true) == 0)
            {
                return (true);
            }
            return (false);
        }

        private PlayList getExistingPlaylist(String name)
        {
            foreach (PlayList item in playLists)
            {
                if (String.Compare(item.getName(), name, true) == 0)
                    return (item);
            }
            if (String.Compare(name, "Library", true) == 0)
            {
                return (libView.library);
            }
            return (null);
        }

        #region "Envent"
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (playLists != null)
            {
                String playListName = capturePlayListName.Text;

                if (String.IsNullOrEmpty(playListName) == false && checkIfPlayListExists(playListName) == false)
                {
                    PlayList newPlayList = new PlayList(playListName);

                    playLists.Add(newPlayList);

                    TreeViewItem newChild = new TreeViewItem();

                    newChild.Header = playListName;

                    libView.playListsItem.Items.Add(newChild);
                    libView.switchToPlayList(newPlayList);
                    libView.serialiseTreeViewItems();
                }
                else
                {
                    PlayList playlist = getExistingPlaylist(playListName);
                    libView.switchToPlayList(playlist);
                }
            }

            Close();
        }
        #endregion
    }
}
